#ifndef _AUTHORIZATION_H
#define _AUTHORIZATION_H

#include <string>
#include <vector>
#include <map>
#include <ctime>

using namespace std;

#define LIVE_TIME_MS	5*60
#define MAX_BUF 1024
#define DEFAULT_USER_LIST_FILE ".\\user_list.txt"
#define DEFAULT_USER_COOKIES_LIST_FILE ".\\user_cookies_list.tmp"

class CAuthorization
{
	typedef struct RECORD
	{
		string id;
		time_t last_time;
	};

private:
	string name;
	string pass;
	string cookie;
	string id;

	map<string, string> user_list;
	map<string, RECORD> user_cookies_list;

public:
	CAuthorization();
	~CAuthorization();

	char *analyze(const char *method, 
		const char *name, const char *password, 
		char *cookies = NULL, const int cookies_size = 0);

	bool load_user_list(const string path = DEFAULT_USER_LIST_FILE);

private:
	bool load_user_cookies_list(const string path = DEFAULT_USER_COOKIES_LIST_FILE);
	bool save_user_cookies_list(const string path = DEFAULT_USER_COOKIES_LIST_FILE);

private:
	bool is_user_from_file(const string user_name, const string user_pass);
	bool is_valid_cookie(const string user_cookie);
	string gen_cookie(const time_t cur_time);

	string reg_user_name(const string name);
	bool delete_expired_cookie();
	bool prolong_cookie(const string cookie);

	map<string, RECORD>::iterator get_rec_by_user_name(const string user_name);
	map<string, RECORD>::iterator get_rec_by_cookie(const string cookie);

	string get_id_from_cookie(const string cookie);
	string set_id_in_cookie(const string id);

public:
	string get_cookie_by_user_name(const string user_name);
	string get_user_name_by_cookie(const string cookie);

};

#endif 